package com.lanxin.oa.qx.service;

import com.lanxin.bean.QxFunction;
import com.lanxin.bean.QxRole;
import com.lanxin.bean.ZzEmployee;
import com.lanxin.oa.qx.dao.QxroleDao;
import com.lanxin.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class QxroleServiceImpl implements QxroleService {

    @Autowired
    private QxroleDao qxroleDao;

    @Override
    public PageBean<QxRole> selectRole(Integer page, Integer pageSize) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 4;
        }
        return qxroleDao.selectRole(page,pageSize);
    }

    @Override
    public void eidsRole(QxRole role) {
            qxroleDao.add(role);
    }

    @Override
    public QxRole seletcById(Integer oid) {
        return qxroleDao.selectByIdRole(oid);
    }

    @Override
    public void updateRole(QxRole role) {
        QxRole qxRole = qxroleDao.selectByIdRole(role.getOid());
        qxRole.setRoleName(role.getRoleName());
        qxRole.setDescription(role.getDescription());
        qxroleDao.update(qxRole);
    }

    @Override
    public PageBean<QxRole> pageRole(Integer page, Integer pageSize, String roleName) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 4;
        }
        return qxroleDao.pageRole(page, pageSize, roleName);
    }

    @Override
    public QxRole selectRoleUser(Integer oid) {
        return qxroleDao.selectRoleUser(oid);
    }

    @Override
    public void insertRoleUser(Integer roid,String eids) {
        System.out.println(roid+"  "+eids);
        QxRole role = qxroleDao.selectRoleUser(roid);
        Set<ZzEmployee> employees = new HashSet<>();
        if(eids!=null){
            String[] emps = eids.split(":");
            for(String e:emps){
                ZzEmployee employee = qxroleDao.selectEmp(e);
                employees.add(employee);
            }
        }
        role.setEmployees(employees);
        qxroleDao.update(role);
    }

    @Override
    public List<QxFunction> listFunctions() {
        return qxroleDao.functions();
    }

    @Override
    public void insertRoleFunction(Integer oid, String fids) {
        QxRole role = qxroleDao.selectRoleUser(oid);
        Set<QxFunction> functions = new HashSet<>();
        if(fids!=null){
            String[] funs = fids.split(",");
            for(String s:funs){
                QxFunction function = qxroleDao.selectFunction(s);
                functions.add(function);
            }
        }
        role.setFunctions(functions);
        qxroleDao.update(role);
    }

    @Override
    public List<ZzEmployee> myFunctions(Integer eid) {
        return qxroleDao.myFunctions(eid);
    }

    @Override
    public boolean deleteRole(QxRole qxRole) {
        List<QxRole> qxRoleList = qxroleDao.selectRole(qxRole.getOid());
        if (qxRoleList != null && qxRoleList.size() > 0) {
            return false;
        }

        try {
            qxroleDao.delete(qxRole);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
